package com.whmskj.xjlhsc.panel.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.math.MathUtil;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.panel.pojo.dto.ProduceDataDTO;
import com.whmskj.xjlhsc.panel.pojo.entity.ProduceEnergyRatio;
import com.whmskj.xjlhsc.panel.pojo.entity.WorkshopTemperatureAndHumidity;
import com.whmskj.xjlhsc.panel.pojo.vo.ProduceVO;
import com.whmskj.xjlhsc.panel.service.IProduceService;
import com.whmskj.xjlhsc.panel.service.IProduceWenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-13 16:04
 */
@SaIgnore
@RestController
@RequestMapping("/panel/produce")
public class ProduceController {
    @Autowired
    private IProduceService produceService;
    @Autowired
    private IProduceWenService produceWenService;

    @PostMapping
    public Result getProduceData(@RequestBody(required = false) ProduceDataDTO produceDataDTO) {
        if (produceDataDTO == null) {
            produceDataDTO = new ProduceDataDTO();
        }
        if(produceDataDTO.getProcess() == null || produceDataDTO.getProcess().isEmpty()){
            produceDataDTO.setProcess("细纱");
        }
        if(produceDataDTO.getMachinenumber() == null || produceDataDTO.getMachinenumber().isEmpty()){
            produceDataDTO.setMachinenumber("AC10络筒");
        }
        ProduceVO produceData = produceService.getProduceData(produceDataDTO.getProcess());
        List<WorkshopTemperatureAndHumidity> produceWenData = produceWenService.getProduceWenData(produceDataDTO.getMachinenumber());
        //produceWenData倒转
        Collections.reverse(produceWenData);
        produceData.setWorkshopTemperatureAndHumidityList(produceWenData);


        /////
        for (WorkshopTemperatureAndHumidity workshopTemperatureAndHumidity : produceData.getWorkshopTemperatureAndHumidityList()) {
            //小数保留两位小数
            workshopTemperatureAndHumidity.setTemperature(workshopTemperatureAndHumidity.getTemperature() == null ? 0 : Math.round(workshopTemperatureAndHumidity.getTemperature() * 100.0) / 100.0);
            workshopTemperatureAndHumidity.setHumidity(workshopTemperatureAndHumidity.getHumidity() == null ? 0 : Math.round(workshopTemperatureAndHumidity.getHumidity() * 100.0) / 100.0);
        }
        for (ProduceEnergyRatio produceEnergyRatio : produceData.getProduceEnergyRatioList()) {
            //小数保留两位小数
            produceEnergyRatio.setProduce(produceEnergyRatio.getProduce() == null ? 0 : Math.round(produceEnergyRatio.getProduce() * 100.0) / 100.0);
            produceEnergyRatio.setEnergy(produceEnergyRatio.getEnergy() == null ? 0 : Math.round(produceEnergyRatio.getEnergy() * 100.0) / 100.0);
        }
        return Result.success(produceData);
    }

    /*获取工序列表*/
    @GetMapping
    public Result getProcessList() {
        return Result.success(produceService.getProcessList());
    }

    /*空调点位列表*/
    @GetMapping("/MachinenumberList")
    public Result getMachinenumberList() {
        return Result.success(produceService.getMachinenumberList());
    }
}
